package br.com.insejeccg.util.bean.convert;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

import br.com.insejeccg.util.bean.BeanHelperUtil;


/**
 * Faz a conver��o de Objetos em String. Faz um trim() da string
 *
 */
@SuppressWarnings("unchecked")
public class IntegerArrayConverter implements Converter {
    private Object defaultValue;    
  
    /**
    * Create a {@link Converter} that will throw a {@link ConversionException}
    * if a conversion error occurs.
    */
    public IntegerArrayConverter() {
        this.defaultValue = null;
    }

    /**
     * Create a {@link Converter} that will return the specified default value
     * if a conversion error occurs.
     *
     * @param defaultValue The default value to be returned
     */
    public IntegerArrayConverter(final Integer[] defaultValue) {
        this.defaultValue = defaultValue;
    }


    /**
     * Create a {@link Converter} that will return the specified default value
     * if a conversion error occurs.
     *
     * @param defaultValue The default value to be returned
     */
    public IntegerArrayConverter(final int[] defaultValue) {
        this.defaultValue = defaultValue;
    }
    

    /**
     * Convert the specified input object into an output object of the
     * specified type.
     *
     * @param type Data type to which this value should be converted
     * @param value The input value to be converted
     *
     * @exception ConversionException if conversion cannot be performed
     *  successfully
     */
    public Object convert(Class type, Object value) {
    	Object resp = defaultValue;
    	
        // Deal with a null value
        if (value == null) {
            return (defaultValue);
        }

        // Deal with the no-conversion-needed case
        if (value instanceof Integer[] || value instanceof int[]) {
            return (value);
        }else{
        	//convert
	        if(defaultValue instanceof Integer[]){
	        	resp = convertIntegerArray(value);        
	        }else if(defaultValue instanceof int[]){
	        	resp = convertIntArray(value);
	        }
        }
        
        return resp;
    }

	private Object convertIntegerArray(Object value) {
		Integer[] resp = (Integer[]) defaultValue;

        if (value.getClass().isArray()) {
            resp = convertToArrayInteger(value);
        }else
        if (value instanceof Collection) {
            resp = convertToArrayInteger(((Collection) value).toArray());
        }else
        if (value instanceof Map) {
            resp = convertToArrayInteger(((Map) value).values().toArray());
        }        
        return resp;
	}

    private Integer[] convertToArrayInteger(Object value) {
        if (value.getClass().isArray()) {
            int tam = Array.getLength(value);
            Integer[] resp = new Integer[tam];

            Converter converterInteger = BeanHelperUtil.lookup(Integer.class);
            for (int i = 0; i < tam; i++) {
                resp[i] = (Integer)converterInteger.convert(Integer.class, Array.get(value, i));
            }
            return resp;
        }
        return (Integer[])defaultValue;
    }
    
	private Object convertIntArray(Object value) {
		int[] resp = (int[]) defaultValue;

        if (value.getClass().isArray()) {
            resp = convertToArrayInt(value);
        }

        if (value instanceof Collection) {
            resp = convertToArrayInt(((Collection) value).toArray());
        }

        if (value instanceof Map) {
            resp = convertToArrayInt((((Map) value).values().toArray()));
        }
        
        return resp;
	}

    private int[] convertToArrayInt(Object value) {
        if (value.getClass().isArray()) {
            int tam = Array.getLength(value);
            int[] resp = new int[tam];

            Converter converterInteger = BeanHelperUtil.lookup(Integer.class);
            for (int i = 0; i < tam; i++) {
                resp[i] = (Integer)converterInteger.convert(Integer.class, Array.get(value, i));
            }
            return resp;        }

        return (int[])defaultValue;
    }
}
